<?php
namespace Dry\Base;

class Crud {

    private $pdo = null;

    private $sql = null;

    public function __construct($pdo, $sql)
    {
        $this->pdo = $pdo;
        $this->sql = $sql;
    }

    public function setPdoAttribute($key, $value)
    {
        $this->pdo->setAttribute($key, $value);
        return $this;
    }

    public function switch2assoc()
    {
        return $this->setPdoAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC);
    }

    public function switch2obj()
    {
        return $this->setPdoAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_OBJ);
    }

    public function switch2num()
    {
        return $this->setPdoAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_NUM);
    }

    public function getSql()
    {
        return $this->sql;
    }

    public function fetch($data)
    {
        if(is_string($data)){
            $data = array(
                'sql' => $data,
                'placeholder' => array()
            );
        }
        $sm = $this->pdo->prepare($data['sql']);
        foreach($data['placeholder'] as $k => $v){
            $sm->bindValue($k, $v);
        }
        $sm->execute();
        return $sm->fetch();
    }

    public function fetchAll($data)
    {
        if(is_string($data)){
            $data = array(
                'sql' => $data,
                'placeholder' => array()
            );
        }
        $sm = $this->pdo->prepare($data['sql']);
        foreach($data['placeholder'] as $k => $v){
            $sm->bindValue($k, $v);
        }
        $sm->execute();
        return $sm->fetchAll();
    }

    public function fetchColumn($data)
    {
        if(is_string($data)){
            $data = array(
                'sql' => $data,
                'placeholder' => array()
            );
        }
        $sm = $this->pdo->prepare($data['sql']);
        foreach($data['placeholder'] as $k => $v){
            $sm->bindValue($k, $v);
        }
        $sm->execute();
        return $sm->fetchColumn();
    }

    public function runInsert($data)
    {
        if(is_string($data)){
            $data = array(
                'sql' => $data,
                'placeholder' => array()
            );
        }
        $sm = $this->pdo->prepare($data['sql']);
        foreach($data['placeholder'] as $k => $v){
            $sm->bindValue($k, $v);
        }
        $sm->execute();
        return $this->pdo->lastInsertId();
    }

    public function runUpdate($data)
    {
        if(is_string($data)){
            $data = array(
                'sql' => $data,
                'placeholder' => array()
            );
        }
        $sm = $this->pdo->prepare($data['sql']);
        foreach($data['placeholder'] as $k => $v){
            $sm->bindValue($k, $v);
        }
        $sm->execute();
        return $sm->rowCount();
    }

    public function runDelete($data)
    {
        if(is_string($data)){
            $data = array(
                'sql' => $data,
                'placeholder' => array()
            );
        }
        $sm = $this->pdo->prepare($data['sql']);
        foreach($data['placeholder'] as $k => $v){
            $sm->bindValue($k, $v);
        }
        $sm->execute();
        return $sm->rowCount();
    }

    public function runExecute($data)
    {
        if(is_string($data)){
            $data = array(
                'sql' => $data,
                'placeholder' => array()
            );
        }
        $sm = $this->pdo->prepare($data['sql']);
        foreach($data['placeholder'] as $k => $v){
            $sm->bindValue($k, $v);
        }
        $sm->execute();
    }

    public function insert($table, $data)
    {
        $sql = $this->sql;
        $sql->table($table);
        foreach($data as $k => $v){
            $sql->set($k, $v);
        }
        return $this->runInsert($sql->getInsertSql());
    }

    public function update($table, $data, $id)
    {
        $sql = $this->sql;
        $sql->table($table);
        foreach($data as $k => $v){
            $sql->set($k, $v);
        }
        $sql->where('id', '=', $id);
        return $this->runUpdate($sql->getUpdateSql());
    }

    public function delete($table, $id)
    {
        $sql = $this->sql;
        $sql->table($table);
        $sql->where('id', 'in', $id);
        return $this->runDelete($sql->getDeleteSql());
    }

    public function one($table, $id)
    {
        $sql = $this->sql;
        $sql->table($table);
        $sql->where('id', '=', $id);
        $sql->limit(1, 1);
        return $this->fetch($sql->get());
    }

    public function more($table, $id)
    {
        $sql = $this->sql;
        $sql->table($table);
        $sql->where('id', 'in', $id);
        return $this->fetchAll($sql->get());
    }

}
?>
